package com.chen.palmar.project.init;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.chen.palmar.R;
import com.chen.palmar.base.BaseActivity;
import com.chen.palmar.project.set.SettingActivity;
import com.primb.androidlibs.net.entity.MessageEvent;
import com.tencent.bugly.beta.Beta;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {
    @Bind(R.id.iv_one)
    ImageView ivOne;
    @Bind(R.id.tv_one)
    TextView tvOne;
    @Bind(R.id.iv_two)
    ImageView ivTwo;
    @Bind(R.id.tv_two)
    TextView tvTwo;
    @Bind(R.id.iv_three)
    ImageView ivThree;
    @Bind(R.id.tv_three)
    TextView tvThree;
    @Bind(R.id.iv_four)
    ImageView ivFour;
    @Bind(R.id.tv_four)
    TextView tvFour;
    @Bind(R.id.iv_five)
    ImageView ivFive;
    @Bind(R.id.tv_five)
    TextView tvFive;
    private FragmentManager fragmentManager;
    private HomeFragment homeFragment;
    private AgencyFragment agencyFragment;
    private ProducerFragment producerFragment;
    private SetFragment setFragment;
    private String userName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        init();
    }

    @Override
    public void init() {
        fragmentManager = getSupportFragmentManager();
        setChoiceItem(0);
        //        更新检查
        //        参数1：isManual 用户手动点击检查，非用户点击操作请传false
        //        参数2：isSilence 是否显示弹窗等交互，[true:没有弹窗和toast] [false:有弹窗或toast]
        Beta.checkUpgrade(false, false);

        Intent intent = getIntent();
        userName = intent.getStringExtra("userName");
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    // 定义一个选中一个item后的处理
    public void setChoiceItem(int index) {
        // 重置选项+隐藏所有Fragment
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        clearChoice();
        hideFragments(transaction);
        switch (index) {
            case 0:
                ivOne.setImageResource(R.mipmap.tab_home_selected);
                tvOne.setTextColor(ContextCompat.getColor(this, R.color.tab_bar_bg));
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.layout_content, homeFragment).show(homeFragment);
                } else {
                    transaction.show(homeFragment);
                }
                break;
            case 2:
                ivThree.setImageResource(R.mipmap.tab_search_selected);
                tvThree.setTextColor(ContextCompat.getColor(this, R.color.tab_bar_bg));
                if (agencyFragment == null) {
                    agencyFragment = new AgencyFragment();
                    transaction.add(R.id.layout_content, agencyFragment).show(agencyFragment);
                } else {
                    transaction.show(agencyFragment);
                }

                break;
            case 3:
                ivFour.setImageResource(R.mipmap.tab_tools_selected);
                tvFour.setTextColor(ContextCompat.getColor(this, R.color.tab_bar_bg));
                if (producerFragment == null) {
                    producerFragment = new ProducerFragment();
                    transaction.add(R.id.layout_content, producerFragment).show(producerFragment);
                } else {
                    transaction.show(producerFragment);
                }
                break;
            case 4:
                ivFive.setImageResource(R.mipmap.tab_mine_selected);
                tvFive.setTextColor(ContextCompat.getColor(this, R.color.tab_bar_bg));
                if (setFragment == null) {
                    setFragment = new SetFragment();
                    transaction.add(R.id.layout_content, setFragment).show(setFragment);
                } else {
                    transaction.show(setFragment);
                }
                break;
            default:
        }
        transaction.commit();
    }

    // 定义一个重置所有选项的方法
    public void clearChoice() {
        ivOne.setImageResource(R.mipmap.tab_home);
        tvOne.setTextColor(ContextCompat.getColor(this, R.color.gray_898989));
        ivTwo.setImageResource(R.mipmap.tab_approval);
        tvTwo.setTextColor(ContextCompat.getColor(this, R.color.gray_898989));
        ivThree.setImageResource(R.mipmap.tab_search);
        tvThree.setTextColor(ContextCompat.getColor(this, R.color.gray_898989));
        ivFour.setImageResource(R.mipmap.tab_tools);
        tvFour.setTextColor(ContextCompat.getColor(this, R.color.gray_898989));
        ivFive.setImageResource(R.mipmap.tab_mine);
        tvFive.setTextColor(ContextCompat.getColor(this, R.color.gray_898989));
    }

    // 隐藏所有的Fragment,避免fragment混乱
    private void hideFragments(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (producerFragment != null) {
            transaction.hide(producerFragment);
        }
        if (agencyFragment != null) {
            transaction.hide(agencyFragment);
        }
        if (setFragment != null) {
            transaction.hide(setFragment);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitTime();
        }
        return false;
    }

    private long exitTime = 0;

    private void exitTime() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            showToast("再按一次退出程序");
            exitTime = System.currentTimeMillis();
        } else {
            /**
             * 退出程序
             */
            this.finish();
            exit();
        }
    }

    @OnClick({R.id.one_layout, R.id.two_layout, R.id.three_layout, R.id.four_layout, R.id.five_layout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.one_layout:
                setChoiceItem(0);
                break;
            case R.id.two_layout:
                setChoiceItem(1);
                break;
            case R.id.three_layout:
                setChoiceItem(2);
                break;
            case R.id.four_layout:
                setChoiceItem(3);
                break;
            case R.id.five_layout:
                setChoiceItem(4);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MessageEvent messageEvent) {
        switch (messageEvent.getEventType()) {
            case SettingActivity.EVENT_SET_CODE:
                this.finish();
                break;
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        //取消注册事件
        EventBus.getDefault().unregister(this);
    }
}
